package com.itheimamobileguard.activitys;

import java.util.List;

import com.example.mobile.R;
import com.itheimamobileguard.db.dao.BlackNumberDao;
import com.itheimamobileguard.domain.BlockNumberInfo;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CallSmsSafeActivity extends Activity {
	private ListView lv_callsms_safe;
	private BlackNumberDao dao;
	private List<BlockNumberInfo> infos;
	private LinearLayout ll_add_number_tips;
	private LinearLayout ll_loading;
	private CallSmsSafeAdapter adapter;
	/**
	 * 开始获取的位置
	 */
	private int startIndex = 0;
	/**
	 * 最多几条
	 */
	private int maxCount = 20;
	
	private int totalCount = 0;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			ll_loading.setVisibility(View.INVISIBLE);
			if (infos.size() == 0) {
				// 没有数据，显示添加数据的提醒
				ll_add_number_tips.setVisibility(View.VISIBLE);
			} else {
				// 显示数据
				if (adapter == null) {
					adapter = new CallSmsSafeAdapter();
					lv_callsms_safe.setAdapter(adapter);
				}else {
					//已经存在，应为数据适配器里面的数据已经变化，刷新界面
					adapter.notifyDataSetChanged();
				}
				
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initUI();
		fillData();
	}

	/**
	 * 填充数据
	 */
	private void fillData() {
		dao = new BlackNumberDao(this);
		totalCount = dao.getTotalNumber();
		// 耗时的操作，应该放在子线程里面执行
		ll_loading.setVisibility(View.VISIBLE);
		new Thread() {
			public void run() {
				if (infos == null) {
					infos = dao.findPart2(startIndex, maxCount);
				}else {
					infos.addAll(dao.findPart2(startIndex, maxCount));
				}
				
				handler.sendEmptyMessage(0);
			}
		}.start();
	}

	/**
	 * 初始化UI
	 */
	private void initUI() {
		setContentView(R.layout.activity_callsms_safe);
		ll_add_number_tips = (LinearLayout) findViewById(R.id.ll_add_number_tips);
		lv_callsms_safe = (ListView) findViewById(R.id.lv_callsms_safe);
		ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
		lv_callsms_safe.setOnScrollListener(new OnScrollListener() {
			// 滚动状态发送变化调用的方法
			//
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_IDLE: // 禁止状态
					// 判断是否是最后一个条目
					int lastPosition = lv_callsms_safe.getLastVisiblePosition();
					System.out.println("最后一个为" + lastPosition);
					if (lastPosition == infos.size() - 1) {
						//加载更多的数据
						startIndex+=maxCount;
						if (startIndex >=totalCount) {
						Toast.makeText(getApplicationContext(),"没有更多数据", 0).show();
						return;
						}
						fillData();
					}
					break;
				}
			}

			// 只要滚动，就会调用方法
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

			}
		});
	}

	public class CallSmsSafeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return infos.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			if (convertView == null) {
				view = View.inflate(CallSmsSafeActivity.this,
						R.layout.item_callsms, null);
			} else {
				view = convertView;
			}
			TextView tv_phone = (TextView) view
					.findViewById(R.id.tv_item_phone);
			TextView tv_mode = (TextView) view.findViewById(R.id.tv_item_mode);
			BlockNumberInfo info = infos.get(position);
			tv_phone.setText(info.getNumber());
			String mode = info.getMode();
			if ("1".equals(mode)) {
				tv_mode.setText("全部拦截");
			} else if ("2".equals(mode)) {
				tv_mode.setText("短信拦截");
			} else if ("3".equals("电话拦截")) {
				tv_mode.setText("电话拦截");
			}
			return view;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
	}
	
	public void addBlackNumber(View view){
		
	}
}
